package com.zx.mobile.controller;

import com.zx.map.dao.model.OrdLog;
import com.zx.map.service.OrdLogService;
import com.zx.map.service.domain.nocard.NocardPayReturnDomain;
import com.zx.map.service.model.OrdLogModel;
import com.zx.map.utils.MyBeanUtils;
import com.zx.mobile.domain.nocard.MobaoCheckSmsResponse;
import com.zx.mobile.service.proxy.NocardPayProxy;
import com.zx.platform.utils.DateUtils;
import com.zx.platform.utils.HttpClientCommon;
import com.zx.platform.utils.StrUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.List;

/**
 * Created by bibei on 2017/3/29.
 * 轮训异常订单
 */
@Controller
@RequestMapping("/order/exception")
public class MobaoExceptionOrderController {
	private static Logger log = Logger.getLogger(MobaoExceptionOrderController.class);
	@Autowired
	private OrdLogService ordLogService;

	@RequestMapping("/scanMobaoOrdLog")
	public void scanMobaoOrdLog() {
		try {
		log.debug("定时查询摩宝异常订单开始");
		OrdLog ordLog = new OrdLog();
		ordLog.setPaymentType("mb");
		ordLog.setTransStatus("N");
		ordLog.setCreateDate(DateUtils.addDateByMinute(new Date(),-30));

		List<OrdLog> ordLogList = ordLogService.getListByRecord(ordLog);
		log.debug("摩宝异常订单数量:"+ordLogList.size());
		for (OrdLog ordLogTmp : ordLogList) {
			NocardPayReturnDomain nocardPayReturnDomain = new NocardPayReturnDomain();
			nocardPayReturnDomain.setOrderId(ordLog.getOrdId());
			nocardPayReturnDomain.setAmount(""+ordLog.getPaymentAmt());
			nocardPayReturnDomain.setYbOrderId(ordLog.getPaymentId());
			String url=NocardPayProxy.getMobaoQueryStatusUrl(ordLogTmp);
			MobaoCheckSmsResponse mobaoCheckSmsResponse = new HttpClientCommon().getResult(MobaoCheckSmsResponse.class, url);
			log.debug("摩宝交易查询返回："+MyBeanUtils.beanToJson(mobaoCheckSmsResponse));
			nocardPayReturnDomain.setPaymentType("mb");
			if(mobaoCheckSmsResponse.getStatus().equals("00")) {
				if(mobaoCheckSmsResponse.getRefCode().equals("00")) {
					nocardPayReturnDomain.setStatus("S");
				} else if(mobaoCheckSmsResponse.getRefCode().equals("02")) {
					nocardPayReturnDomain.setStatus("F");
				}
				nocardPayReturnDomain.setYbOrderId(mobaoCheckSmsResponse.getKsPayOrderId());
			} else {
				nocardPayReturnDomain.setStatus("F");
			}
			//强制关单  超过15分钟
			Date date=DateUtils.addDateByMinute(new Date(),-15);
			if(StrUtils.isBlank(nocardPayReturnDomain.getStatus())&&ordLogTmp.getCreateDate().compareTo(date)<=0){
				nocardPayReturnDomain.setStatus("F");
			}
			//后台通知结果
			if(StrUtils.isNotBlank(nocardPayReturnDomain.getStatus())) {
				OrdLogModel ordLogModel= MyBeanUtils.map(ordLogTmp,OrdLogModel.class);
				new HttpClientCommon().getResultByGet(NocardPayProxy.getOrderNotifyPayUrl(nocardPayReturnDomain, ordLogModel));
			}
		}
		log.debug("查询摩宝异常订单结束");
		}catch (Exception e){
			log.debug("摩宝处理异常订单异常");
		}
	}

}
